//
//  LaundryBasketPageController.m
//  WashForU
//
//  Created by 王俊 on 15/11/25.
//  Copyright © 2015年 王俊. All rights reserved.
//

#import "LaundryBasketPageController.h"

@interface LaundryBasketPageController ()
@property(nonatomic,strong)CartSingleton                   *singleton;
@property(nonatomic,strong)UITableView                     *shelfTableView;
@property(nonatomic,strong)NSMutableArray<ClotheInCart *>  *itermInCart;
@property(nonatomic,strong)LaundryBasketBottomView         *bottomView;
@property(nonatomic)int                                    sumCost;



@end

@implementation LaundryBasketPageController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self customViewSetUp];
    [self customConstrainsAdd];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.sumCost = [self getSumCost];
    self.noClothBackgroundImage.hidden = (self.singleton.itermInCart.count > 0 ? true : false);
    self.shelfTableView.hidden         = (self.singleton.itermInCart.count > 0 ? false : true);
    self.bottomView.hidden             = ((self.sumCost == 0 || self.singleton.itermInCart.count == 0 ) ? true :false);
    [self updateData];
    [self.shelfTableView reloadData];
}
/*****************添加视图组件********************/
- (void)customViewSetUp
{
    self.titleLabel.text = @"洗衣篮";
    self.titleLabel.font = [UIFont CustomFont:20];
    self.titleLabel.textColor = [UIColor GrayColor_88];
    self.singleton = [CartSingleton shareCartSingleton];
    self.bottomView = [[LaundryBasketBottomView alloc]initWithView];

    self.noClothBackgroundImage = [[UIImageView alloc]init];
    self.noClothBackgroundImage.image = [UIImage imageNamed:@"空洗衣篮背景图"];
    //self.shelfTableView = [[UITableView alloc]init];
    self.shelfTableView = [[UITableView alloc]init];//WithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.shelfTableView.backgroundColor = [UIColor clearColor];
    self.shelfTableView.delegate = self;
    self.shelfTableView.dataSource = self;
    [self.shelfTableView registerClass:[ShelfTableViewCell class] forCellReuseIdentifier:REUSECELLLID];
    [self.shelfTableView setExtraCellLineHidden];

    
    
    
    
    [self.view addSubview:_noClothBackgroundImage];
    [self.view addSubview:self.shelfTableView];
    [self.view addSubview:self.bottomView];
    
    
}
/*******************添加约束**********************/
- (void)customConstrainsAdd
{
    [_noClothBackgroundImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.upView.mas_bottom);
        make.leading.trailing.bottom.equalTo(self.view);
    }];
    [self.shelfTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.noClothBackgroundImage);
        make.top.equalTo(self.noClothBackgroundImage).offset(1);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-50);
        make.height.mas_equalTo(60);
    }];

}
- (void)updateData
{
    self.itermInCart = self.singleton.itermInCart;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
/*******************DataSource设置**********************/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section){
    case 0 : return self.singleton.itermInCart.count;
    case 1 : return 2;
    default: return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ShelfTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:REUSECELLLID];
        cell = [cell initWithData:self.singleton.itermInCart[indexPath.row]];
        [cell.counter.minusBtn addTarget:self action:@selector(minusButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.counter.plusBtn addTarget:self action:@selector(plusButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        return cell;

    }else{
        TableViewCell4Sec1 *cell = [[TableViewCell4Sec1 alloc]init];
        switch (indexPath.row) {
            case 0:  cell = [cell initWithHeaderTitle:@"洗衣券" andTaillerTitle:@"点击使用"];
                break;
            case 1:  cell = [cell initWithHeaderTitle:@"用户信息" andTaillerTitle:@"填写"];
            default:
                break;
        }
        return cell;
    }


}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section){
        case 0 : return 90;
        case 1 : return 50;
        default: return 0;
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    switch (section){
        case 0 : return 0.01;
        default: return 20;
    }}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    switch (section){
        case 0 : return 50;
        default: return 0;
    }
    
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    self.sumCost = [self getSumCost];
    self.bottomView.totalNumberLabel.text = [NSString stringWithFormat:@"%d元",self.sumCost];
    self.bottomView.hidden = ((self.sumCost == 0 || self.singleton.itermInCart.count == 0 ) ? true :false);
    FooterView4Sec0 *vc = [[FooterView4Sec0 alloc]initWithIncome: self.sumCost];
    return vc;
    
}

- (void)minusButtonClicked:(UIButton*)sender
{
    ShelfTableViewCell    *cell = (ShelfTableViewCell*)sender.superview.superview;
    NSIndexPath           *path = [self.shelfTableView indexPathForCell:cell];
    
    
    if (self.singleton.itermInCart[path.row].numb > 0) {
        NSLog(@"减前%d",self.singleton.itermInCart[path.row].numb);
        self.singleton.itermInCart[path.row].numb --;
        NSLog(@"减后%d",self.singleton.itermInCart[path.row].numb);
        [self.shelfTableView reloadData];
    }
    //NSLog(@"第%ld行'-'被点击！",(long)path.row);
    
}

- (void)plusButtonClicked:(UIButton*)sender
{
    ShelfTableViewCell    *cell = (ShelfTableViewCell*)sender.superview.superview;
    NSIndexPath           *path = [self.shelfTableView indexPathForCell:cell];
    self.singleton.itermInCart[path.row].numb ++;
    [self.shelfTableView reloadData];
    //NSLog(@"第%ld行'+'被点击！",(long)path.row);
    
}

- (int)getSumCost
{
    int sum = 0;
    for (ClotheInCart *inCartClotheInfo in self.singleton.itermInCart){
        sum += inCartClotheInfo.numb * ((int)inCartClotheInfo.clotheData.price);
        
        
    }
    return sum;

}

@end

